package com.hnair.bot.common.utils;

import org.apache.commons.beanutils.Converter;

import java.text.DateFormat;
import java.text.DateFormatSymbols;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class DateUtils implements Converter{
	private final static SimpleDateFormat df_datetime = new SimpleDateFormat(
			"yyyyMMddHHmmss");

	private final static SimpleDateFormat df_date = new SimpleDateFormat(
			"yyyyMMdd");

	private final static SimpleDateFormat df_date10 = new SimpleDateFormat(
			"yyyy-MM-dd");
	private final static SimpleDateFormat df_date20 = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm:ss");
	
	private static final String DATE      = "yyyy-MM-dd";
	private static final String DATETIME  = "yyyy-MM-dd HH:mm:ss";
	private static final String TIMESTAMP = "yyyy-MM-dd HH:mm:ss.SSS";
	
	public static String getString20(Date date) {
		if (date != null) {
			return df_date20.format(date);
		} else {
			return null;
		}
	}
	public static String getDateTime(Date date) {
		if (date != null) {
			return df_datetime.format(date);
		} else {
			return null;
		}
	}

	public static Date getDateTime(String dtsrt) {
		try {
			return df_datetime.parse(dtsrt);
		} catch (ParseException e) {
			return null;
		}
	}

	public static String getDate(Date date) {
		if (date != null) {
			return df_date.format(date);
		} else {
			return null;
		}
	}

	public static String getDate10(Date date) {
		if (date != null) {
			return df_date10.format(date);
		} else {
			return null;
		}
	}
	
	public static Date getStringToDate10(String str) throws ParseException {
		if (str != null) {
			return df_date10.parse(str);
		} else {
			return null;
		}
	}

	public static Date getDate(String dtsrt) {
		try {
			return df_date.parse(dtsrt);
		} catch (ParseException e) {
			return null;
		}
	}
	
	public static Date getDate2(String dtsrt){
		try {
			return df_date10.parse(dtsrt);
		} catch (ParseException e) {
			return null;
		}
	}
	public static Date getDate3(String dtsrt){
		try {
			return df_date20.parse(dtsrt);
		} catch (ParseException e) {
			return null;
		}
	}	
	
	public static boolean verifyTime(String startTime, String endTime) {
		if (startTime != null && endTime != null) {
			String curDateTime = getDateTime(new Date());
			if (curDateTime != null) {
				if (curDateTime.compareTo(startTime) >= 0
						&& curDateTime.compareTo(endTime) <= 0) {
					return true;
				} else {
					return false;
				}
			} else {
				return false;
			}
		} else {
			return false;
		}
	}
	@Override
    public Object convert(Class type, Object value) {
        return toDate(type, value);
    }
 
    public static Object toDate(Class type, Object value) {
        if (value == null || "".equals(value))
            return null;
        if (value instanceof String) {
            String dateValue = value.toString().trim();
            int length = dateValue.length();
            if (type.equals(Date.class)) {
                try {
                    DateFormat formatter = null;
                    if (length <= 10) {
                        formatter = new SimpleDateFormat(DATE, new DateFormatSymbols(Locale.CHINA));
                        return formatter.parse(dateValue);
                    }
                    if (length <= 19) {
                        formatter = new SimpleDateFormat(DATETIME, new DateFormatSymbols(Locale.CHINA));
                        return formatter.parse(dateValue);
                    }
                    if (length <= 23) {
                        formatter = new SimpleDateFormat(TIMESTAMP, new DateFormatSymbols(Locale.CHINA));
                        return formatter.parse(dateValue);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return value;
    }
}
